package com.example.springbootmybatispage;

import com.example.springbootmybatispage.model.Employee;
import com.example.springbootmybatispage.request.PostByPageParamRequest;
import com.example.springbootmybatispage.request.PostByUsernameLikeRequest;
import com.example.springbootmybatispage.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 启动类
 */
@SpringBootApplication
@RestController
public class SpringBootMybatisPageApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisPageApplication.class, args);
    }


    @Autowired
    private EmployeeService employeeService;

    /**
     * 接收用户请求的参数
     * @return
     */
    @RequestMapping(value = "/selectRowBounds",method = RequestMethod.GET)
    public List<Employee> home() {
        List<Employee> employeeList = this.employeeService.selectPageByRowBounds(2, 10);
        return employeeList;
    }


    @GetMapping(value = "/selectPageHelper",consumes = "application/json")
    public PageInfo<Employee> pageHelper() {
        return this.employeeService.selectPageByPageHelper(1, 10);
    }

    /**
     * 1.查询的接口暴露给用户分页参数，比如用户想查询第3页的5条数据 --分页查询
     */
    @RequestMapping(value = "/postByPageParams",method = RequestMethod.POST)
    public List<Employee> pageSelectByPageParams(@RequestBody PostByPageParamRequest pageParamRequest){
        return this.employeeService.selectByPageParams(pageParamRequest);
    }


    /**
     * 2.查询的接口暴露给用户比如，根据用户名模糊查询username 包含test的用户
     * @param postByUsernameLikeRequest
     * @return
     */
    @PostMapping(value = "/postByUsername")
    public List<Employee> getByUsernameLike(@RequestBody PostByUsernameLikeRequest postByUsernameLikeRequest){
        return this.employeeService.getByUsernameLike(postByUsernameLikeRequest);
    }

    @PostMapping(value = "/postByEmail")
    public List<Employee> getByEmailLike(@RequestBody PostByUsernameLikeRequest postByUsernameLikeRequest){
        return this.employeeService.getByEmailLike(postByUsernameLikeRequest.getEmail());
    }



}
